package wechat.service.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import wechat.handler.resources.AppFilter;

/**
 * Servlet Filter implementation class Base
 */

@WebFilter(filterName="BaseFilter",urlPatterns="/*")
public class BaseFilter implements Filter {

	private Logger log = LoggerFactory.getLogger(BaseFilter.class);
	
    /**
     * Default constructor. 
     */
    public BaseFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see BaseFilter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see BaseFilter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpServletRequest = (HttpServletRequest)request;
		
		String requestURI = httpServletRequest.getRequestURI();
		String method = httpServletRequest.getMethod();
		log.info("BaseFilter URI=" + requestURI + " method=" + method);
		
		request.setAttribute("appid", AppFilter.getAppId(requestURI));
		chain.doFilter(request, response);
	}

	/**
	 * @see BaseFilter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
